#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

int read(){
	int x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 1e5+10;

int nodenum = 0,ls[N<<2],rs[N<<2],mx,val[N<<2],n,k,rt[N];
ll ans;

struct radio{int x,r,f;}s[N];
bool operator < (radio a,radio b){return a.r > b.r;}

void add(int &o,int pos,int l = 1,int r = mx){
	if(!o) o = ++nodenum; val[o]++;  if(l == r) return ;
	int mid = l+r>>1; if(pos <= mid) add(ls[o],pos,l,mid);else add(rs[o],pos,mid+1,r);
}

int query(int o,int xl,int xr,int l = 1,int r = mx){
	if(!o) return 0; if(l == xl && r == xr) return val[o];
	int mid = l+r>>1; if(xr <= mid) return query(ls[o],xl,xr,l,mid);
	else if(xl > mid) return query(rs[o],xl,xr,mid+1,r);
	else return query(ls[o],xl,mid,l,mid) + query(rs[o],mid+1,xr,mid+1,r);
}

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,k = in;
	for(int i = 1;i <= n;i++) s[i].x = in,s[i].r = in,s[i].f = in,mx = max(s[i].x,mx);
	sort(s+1,s+n+1);
	for(int i = 1;i <= n;i++){
		for(int j = s[i].f-k;j <= s[i].f + k;j++) ans += query(rt[j],max(s[i].x-s[i].r,0),s[i].x+s[i].r);
		add(rt[s[i].f],s[i].x);
	}
	printf("%lld\n",ans);
	return 0;
}

